package ossOutput;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static configure.OutputConfig.*;
import static moveMysql.Constant.JDBC_DRIVER;
import static ossOutput.OutputFileConstant.*;

public class FileOutput {

    private static Connection conn = null;
    private static Statement stmt = null;


    public static void main(String[] args) {
        LinkedList<Long> caseId = new LinkedList<>();
        caseId.add(new Long(1467));
        caseId.add(new Long(1468));
        caseId.add(new Long(1469));
        caseId.add(new Long(1470));
        caseId.add(new Long(1471));
        LinkedList<Long> targetId = new LinkedList<>();
        targetId.add(new Long(1336));
        targetId.add(new Long(1335));
        targetId.add(new Long(1337));
        targetId.add(new Long(1338));
        targetId.add(new Long(1341));

        fileOut(caseId, targetId);
    }

    public static void fileOut() {
        //连接源mysql
        try {
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);
            //连接数据库
            conn = DriverManager.getConnection(DB_URL_SOURCE, USER_SOURCE, PASS_SOURCE);
        } catch (Exception e) {
            System.out.println("源数据库连接失败:" + e.getMessage());
        }
        //查询
        try {
            //实例化Statement对象
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(getUrlSql);
            saveData(rs);
            rs = stmt.executeQuery(getTargetUrlSql);
            while (rs.next()) {
                String tempUrl = rs.getString(1);
                String tempName = tempUrl.replace(OSS_URL, "");
                /*String[] split = tempUrl.split("/");
                String tempName = split[split.length - 1];
                System.out.println(tempUrl);*/
                downloadByUrl(tempUrl, filePath + "/" + rootPath, tempName);
            }
            rs.close();
            // 完成后关闭
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void fileOut(List<Long> caseIds, List<Long> targetIds) {
        //连接源mysql
        try {
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);
            //连接数据库
            conn = DriverManager.getConnection(DB_URL_SOURCE, USER_SOURCE, PASS_SOURCE);
        } catch (Exception e) {
            System.out.println("源数据库连接失败:" + e.getMessage());
        }
        //查询
        try {
            //实例化Statement对象
            stmt = conn.createStatement();
            for (Long caseId : caseIds) {
                System.out.println("caseId:" + caseId);
                PreparedStatement pstmt = (PreparedStatement) (conn.prepareStatement(getUrlSqlByCase));
                pstmt.setLong(1, caseId);
                //pstmt.executeUpdate();
                ResultSet rs = pstmt.executeQuery();
                saveData(rs);
            }
            for (Long targetId : targetIds) {
                System.out.println("targetId:" + targetId);
                PreparedStatement pstmt = (PreparedStatement) (conn.prepareStatement(getTargetUrlSqlByTargetId));
                pstmt.setLong(1, targetId);
                //pstmt.executeUpdate();
                ResultSet rs = pstmt.executeQuery();
                if (rs.next()) {
                    String tempUrl = rs.getString(1);
                    String tempName = tempUrl.replace(OSS_URL, "");
                    /*System.out.println(tempUrl);
                    String[] split = tempUrl.split("/");
                    System.out.println(split.length);
                    String tempName = split[split.length - 1];*/
                    //System.out.println(tempName);
                    downloadByUrl(tempUrl, filePath + "/" + rootPath, tempName);
                }
            }
            // 完成后关闭
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void saveData(ResultSet rs) throws Exception {
        while (rs.next()) {
            if (null != rs.getString(1)) {
                //正则匹配

                Matcher m = Pattern.compile("\"http://([^\"]*)\"").matcher(rs.getString(1));

                //获取url

                while (m.find()) {
                    String tempUrl = m.group().replace("\"", "");
                    /*String[] split = tempUrl.split("/");
                    String tempName = split[split.length - 1];
                    System.out.println(tempUrl);*/
                    String tempName = tempUrl.replace(OSS_URL, "");
                    downloadByUrl(tempUrl, filePath + "/" + rootPath, tempName);
                }

            }

        }
    }

    public static boolean downloadByUrl(String url, String filePath, String fileName) throws Exception {
        if (fileName == null)
            return false;
        File directory = new File(filePath);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        String downloadPath = FilenameUtils.concat(filePath, fileName);


        URL url1 = new URL(url);
        try {
            File file = new File(downloadPath);
            FileUtils.copyURLToFile(url1, file);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
